package com.neuedu.web;

import com.alibaba.fastjson.JSON;
import com.neuedu.service.ArticleService;
import com.neuedu.service.ArticleServiceImpl;
import com.neuedu.utils.PageInfo;
import com.neuedu.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "PageLoadServlet",urlPatterns = "/page/loadIndexInfo")
public class PageLoadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String pageIndex = request.getParameter("pageIndex"); // 0
        String pageSize = request.getParameter("pageSize"); // 5

        PageUtils pageUtils = new PageUtils();
        pageUtils.setPageIndex(Integer.parseInt(pageIndex)*Integer.parseInt(pageSize));
        pageUtils.setPageSize(Integer.parseInt(pageSize));

        // 调service完成分页查询
        ArticleService articleService = new ArticleServiceImpl();
        List<Map<String,Object>> list = articleService.getPage(pageUtils);
        Long total = articleService.getTotal();

        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(list);
        pageInfo.setTotal(total);
        response.getWriter().println(JSON.toJSONString(pageInfo));
    }
}
